<?php
/**
 * Created By Project
 * Author: 陈东
 * Copyright：陈东
 * Source Code：陈东
 * Explain：I shall not authorize for any other commercial purposes, law breakers must be prosecuted.
 */
namespace Home\Controller;
use Think\Controller;
use Home\Model\PlateModel;

class BaseController extends Controller {

	/**
	 * 实例化模型对象
	 */
	protected function newmodle(){
		return new PlateModel();
	}
	
	public function _initialize(){
		/* 禁止这些微信访问 */
		$get_userinfo 	= $_COOKIE['user_userinfo'];
		$forbidstr 		= explode(',',C('JINZHI'));
		foreach($forbidstr as $v){
			if(trim($v)==$get_userinfo) die;
		}
		$jumptoid 	= I('get.jumptoid');
		$rootid_tmp	= I('get.rootid');
		$array_d_r 	= $this->get_rootid_domainid($rootid_tmp);
		/* 获取rootid和domainid */
		$rootid 	= $array_d_r['id'];
		$domainid 	= $array_d_r['domainid'];
		$this->assign('domainid', $domainid);
		$this->assign('rootid', $rootid);
		/* 记录访问者IP */
		$this->record_visitor_ip($rootid);

		/* 统计此码的扫码次数 */
		$this->totalnum($rootid);
		/* 判断是否是PC还是移动端 */
		$ismobile = ismobile() ? 1 : 0;
		$this->assign('ismobile',$ismobile);
		/* 获取用户的微信openid */
		$this->assign('userinfo',$_COOKIE['user_userinfo']);
		/* 公用变量 */
		$getid = I('get.id');
		$where['state'] = 1;
		$article = D(C('TABLE_ARTICLE'));
		$articleReuslt = $article->where($where)->order('weight desc')->select();
		$this->assign('articleReuslt',$articleReuslt);
		/* 企业根据ID获取有没有绑定的ID */
		$this->getidbybinid($getid);
		/* 获取submenu(分类表)数据 */
		$this->submenu($array_d_r['domainid'], $id);
		/* 缺少参数 提示消息 */
		$errormessage = "<script>alert('缺少参数！');</script>";
		/* 微信分享 */
		$m_signPackage = $this->weixinshare();
		$this->assign('signPackage',$m_signPackage);
		/* 获取当前地址 */
		$this->assign('geturl',getURL());
	}


	/**
	 * 根据版块ID获取版块内容
	 */
	public function plateid_contentlist($plateID){
		$plate = $this->newmodle();
		switch ($plateID){
			//历史上的今天
			case 52:
				$history_list_info = $plate::get_byplateid_content(52);
				$history_id_info = $history_list_info[0];
				$this->assign('history_resource_info', $history_id_info);
				$this->assign('history_resource_list_info', $history_list_info);
				break;
			//微信精选
			case 53:
				$get_weixin_first_info = $plate::get_byplateid_content(53);
				$this->assign('weixin_resource_info', $get_weixin_first_info);
				break;
			//新闻头条
			case 56:
				$get_news_first_info = $plate::get_byplateid_content(56);
				$this->assign('news_resource_info', $get_news_first_info);
				break;
			//最新笑话
			case 57:
				$get_xiaohua_first_info = $plate::get_byplateid_content(57);
				$this->assign('xiaohua_resource_info', $get_xiaohua_first_info);
				break;
			//最新趣图
			case 58:
				$get_qutu_first_info = $plate::get_byplateid_content(58);
				$this->assign('qutu_resource_info', $get_qutu_first_info);
				break;
			default:
				$default_list_info = $plate::get_byplateid_content($plateID);
				$this->assign('default_resource_list_info', $default_list_info);
		}
	}

	/**
	 * 获取自定义版块
	 */
	public function get_custom_plate($plate_id){
		$plate = D(C('TABLE_PLATEMANAGES'));
		$where['id'] = array('in', $plate_id);
		$res = $plate->where($where)->order('plate_sort DESC')->select();
		return $res;
	}

	/**
	 * 根据版块ID获取板块信息
	 */
	protected function get_palte_info($plate_id){
		$plate = D(C('TABLE_PLATEMANAGES'));
		$res = $plate->find($plate_id);
		return $res;
	}

	/**
	 * 获取rootid和domainid
	 */
	public function get_rootid_domainid($rootid_tmp = null){
		$dmid_tmp 	= $this->rootidTodomainid($rootid_tmp);
		$array_d_r 	= $this->getdomaidandrootid($dmid_tmp);
		return $array_d_r;
	}
	
	/**
	 * 判断是否微信联合登陆
	 */
	public function islogin($getURL){
		if(empty($_COOKIE['user_userinfo'])){
			header("Location:".C('LOGINADDRES').$getURL);
			exit;
		}
	}
	
	/**
	 * 记录访问者IP
	 */
	public function record_visitor_ip($ma_id = null){
		$result = false;
		$ip 	= get_client_ip();
		$url 	= getURL();
		if($this->check_visitor_ip($ip, $ma_id, $url)){
			//增加访问次数
			$obj_ip = D(C('TABLE_IP'));
			$res 	= $obj_ip->where(array('ip' => $ip, 'ma_id' => $ma_id, 'url' => $url))->setInc('num', 1);
			$result = $res ? true : false;
		}else{
			if($ip != '60.253.230.74' && $ip != '123.57.69.167' && $ip != '223.255.127.151' && !empty($ip)){
				if(get_domain($url) == "hrtv.cn"){
					$obj_ip = D(C('TABLE_IP'));
					$data['ma_id'] 	= $ma_id;
					$data['ip'] 	= $ip;
					$data['url'] 	= $url;
					$result = $obj_ip->add($data) ? true : false;
				}
			}
		}
		return $result;
	}
	
	/**
	 * 判断是否有相同记录存在
	 */
	public function check_visitor_ip($ip, $ma_id, $url){
		$result = false;
		$obj_ip = D(C('TABLE_IP'));
		$res = $obj_ip->where(array('ip' => $ip, 'ma_id' => $ma_id, 'url' => $url))->count();
		$result = $res ? true : false;
		return $result;
	}

	/**
	 * 统计此码的扫码次数
	 */
	protected function totalnum($rootid){
		if(!empty($rootid)){
			$type = D(C('TABLE_TYPE'));
			$type->where(array('id'=>$rootid))->setInc('num');
		}
	}
	
	/**
	 * 获取控制器和方法
	 */
	public function get_controller_action(){
		return CONTROLLER_NAME."/".ACTION_NAME;
	}
	
	/**
	 * 根据rootid获取domainid
	 */
	public function rootidTodomainid($rootid = null){
		if(!empty($rootid)){
			$type = D(C('TABLE_TYPE'));
			$res = $type->field('domainid')->find($rootid);
			if($res) return $res['domainid'];
		}
	}
	
	public function getdomaidandrootid($domainid = null){
		if($this->get_controller_action() == C('ENTER_ADDRESS')){
			$randcode = $this->getrandma();
			if(!empty($randcode)){
				if(cookie('randcode') != $randcode) cookie('randcode',$randcode);
			}else{
				cookie('randcode',null);
			}
			if(!empty($domainid)){
				$typeinfo = $this->gettyperes($domainid);
			}else{
				$rand = D(C('TABLE_RANDNUMBER'));
				$res = $rand->where(array('ma_randnumber'=>$randcode))->find();
				if(!empty($res)){
					$typeinfo = $this->gettyperes($res['ma_domainid']);
				}
			}
		}
		return $typeinfo;
	}
	
	/**
	 * 获取一物一码唯一的标识串
	 */
	protected function getrandma(){
		$code_tmp = $_SERVER['QUERY_STRING'];
		$n1 = isset($_GET['share']) ? strpos($code_tmp, '&') : strpos($code_tmp, '=');
		$code = isset($_GET['share']) ? strpos($code_tmp, '&') ? substr($code_tmp, 0, $n1) : $code_tmp : strpos($code_tmp, '=') ? substr($code_tmp, 0, $n1) : $code_tmp;
		return rtrim($code, '=');
	}
	
	/**
	 * 根据domainid获取此条信息
	 */
	protected function gettyperes($domainid){
		$typeres = D(C('TABLE_TYPE'));
		$tres 	 = $typeres->where(array('domainid'=>$domainid))->find();
		return $tres;
	}

	/**
	 * 根据rootid获取此条信息
	 */
	public function id_get_type_info($rootid){
		$type = D(C('TABLE_TYPE'));
		$res  = $type -> where(array('id' => $rootid)) -> find();
		return $res;
	}

	protected function getsummenubinid($id){
		$submenures = D(C('TABLE_SUBMENU'));
		$res = $submenures->field('binid')->find($id);
		if($res){
			if(!empty($res['binid'])){
				return $res['binid'];
			}else{
				return false;
			}
		}else{
			return false;
		}
	}
	function weixinshare(){
		include('jssdk.php');
		$jssdk = new \JSSDK("wx30934416968cbcb7", "2a1dabeb57ac5688b33117d29f6ec062");
		$signPackage = $jssdk->GetSignPackage();
		return $signPackage;
	}
	
	/**
	 * 缺少参数 提示消息
	 */
	function errormessage($id){
		$id = I('get.ids');
		if(empty($id)){
			$errormessage = "<script>alert('缺少参数！');</script>";
			return $errormessage;
		}
	}
	
	/**
	 * 企业根据ID获取有没有绑定的ID
	 */
	protected function getidbybinid(){
		$id = I('get.id');
		$submenu = D(C('TABLE_SUBMENU'));
		$res = $submenu->where(array('mtypeid'=>$id))->select();
		if(count($res)>0){
			foreach ($res as $v){
				if($v['classtype']==2 && $v['mparent']==0 && $v['state']==1){
					if(!empty($v['binid'])){
						$getbid = $v['binid'];
					}else{
						//$getbid = $this->getbid($id);
						$getbid = $id;
					}
				}
			}
			$this->assign('getid',$getbid);
		}else{
			$this->assign('getid',$this->getbid($id));
		}
	}
	
	protected function getbid($id){
		$submenu = D(C('TABLE_SUBMENU'));
		$res = $submenu->field('binid')->find($id);
		if(!empty($res['binid'])){
			$getbid = $res['binid'];
		}else{
			$getbid = $id;
		}
		return $getbid;
	}
	
	/*缺少参数 提示消息1*/
	public function alerterror($str){
		echo "<div style=\"text-align:center; font-size:14px;\">".$str."</div>";
		die;
	}
	
	/*获取所有分类 或者 按domain ID获取*/
	function submenu($domainid,$id=null){
		$submenu = D(C('TABLE_SUBMENU'));
		$submenuReuslt = $submenu->where($this->selectwhere($id,$domainid))->order('msort asc')->select();
		if(!empty($submenuReuslt)){
			foreach($submenuReuslt as &$item){
				$item['jumpaddres'] = str_replace('#domainid#',$domainid,$item['jumpaddres']);
			}
		}
		$this->assign('submenuReuslt',$submenuReuslt);
	}
	
	/*获取所有分类 或者 按domain ID获取*/
	function getsubmenu($domainid){
		$id = $this->getdomaidandrootid($domainid)['id'];
		$domainid = $this->getdomaidandrootid($domainid)['domainid'];
		$submenu = D(C('TABLE_SUBMENU'));
		$submenuReuslt = $submenu->where($this->selectwhere($id, $domainid))->order('msort asc')->select();
		return $submenuReuslt;
	}
	
	function selectwhere($id,$domainid){
		$type = D(C('TABLE_TYPE'));
		$typeReuslt = $type->where(array('domainid'=>$domainid))->find();
		if(!empty($typeReuslt)){
			$mid = $typeReuslt['id'];
		}else{
			$mid = null;
		}
		if(!empty($id)){
			$where['mtypeid'] = $id;
		}else{
			if(!empty($mid)){
				$where['mtypeid'] = $mid;
			}else{
				$where = null;
			}
		}
		return $where;
	
	}

	/*根据ID获取**码*/
	function getMa($id=null){
		if(!empty($id)){
			$where['id'] = $id;
		}else{
			$where = null;
		}
		$type = D(C('TABLE_TYPE'));
		$typeReuslt = $type->where($where)->find();
		$this->assign('typeReuslt',$typeReuslt);
		if(!empty(cookie('randcode'))){
			$home = C('WEBDOMAIN').'/?'.cookie('randcode');
		}else{
			$home = C('WEBDOMAIN')."/rootid/".$typeReuslt['id'].'.html';
		}
		/*返回首页*/
		$this->assign('home',$home);
	}
	
	/**
	 * 检测某个码是否被关闭，给与提示！
	 */
	function detectionMaClose($id){
 		$type = D(C('TABLE_TYPE'));
		$typeResult = $type->find($id);
		if($typeResult['state']!=1){
			die($this->alerterror(C("MSG_AUDIT")));
		}
	}
	
	/**
	 * 更新码红包领取次数
	 */
	public function setmahbcishu($marandnumber,$cishu){
		$rand = D(C('TABLE_RANDNUMBER'));
		$data['ma_hb_cishu'] = $cishu;
		$res = $rand->where(array('ma_randnumber'=>$marandnumber))->save($data);
	}
	
	
	
	
	

}